//
//  SetupGame.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 4/9/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include "SetupGame.hpp"
#include "GameWorld.hpp"
#include "MathHelper.hpp"
#include <algorithm>
#include <sstream>
#include "SelectableCategoryColorSystem.hpp"

using namespace Nano;

void SetupGame::Initialize() {
    
    selectableCategories = Parent().world.CreateSystem<SelectableCategorySystem>();
    Parent().world.CreateSystem<SelectableCategoryColorSystem>();
    
    Gui* gui = Parent().gui;
    
    pivot = gui->CreatePivot();
    
    Vector2 backgroundSize(15,8);
    
    GameObject* background = gui->CreateControl(pivot, "Background", -backgroundSize*0.5f, backgroundSize);
    background->GetComponent<Colorable>()->Color = Colour::Black(0.5f);

    for (int i=0; i<4; i++) {
        playerFrame[i]=CreatePlayerFrame(&playerName[i]);
    }
    
    startGameButton = gui->CreateLabelControl(pivot, "Background", 0,Vector2(2,1), Parent().font, "Start", 0.5f);
    startGameButton->GetComponent<Touchable>()->Click += event_handler(this, &SetupGame::StartButtonClicked);
    startGameButton->GetComponent<Material>()->Mask = 1;
    startGameButton->Children()[0]->GetComponent<Material>()->Mask = 1;
    startGameButton->GetComponent<Transform>()->Position = Vector2(0,-3);
    
    categoriesCreated = false;
    
    Disabled();
}

void SetupGame::Update(float dt) {
    CreateCategories();
}

void SetupGame::Render() {

}

void SetupGame::Enabled() {

    Parent().categoryFactory->ClearAll();

    std::vector<GamePlayer*> activePlayers;
    
    for (int i=0; i<4; i++) {
        activePlayers.push_back(&Parent().data.Players[i]);
    }
    
    Vector2 size(6.0f,0.5f);
    Vector2 screenSize(10.0f, 2.5f);
    
    float dY = screenSize.y / activePlayers.size();

    Vector2 position = Vector2(0, -screenSize.y * 0.5f + dY * 0.5f - 1);
    Vector2 delta = Vector2(0, dY);

    for (int i=0; i<4; i++) {
        Vector2 pos = i<activePlayers.size() ? position : Vector2(0,-10);
        playerFrame[i]->GetComponent<Transform>()->Position = pos;
        if (i<activePlayers.size()) {
            playerName[i]->GetComponent<Label>()->Text = activePlayers[i]->Name;
            playerFrame[i]->GetComponent<Colorable>()->Color = activePlayers[i]->color;
        }
        position += delta;
    }
    
    
    
    pivot->ToFront();
    pivot->GetComponent<Transform>()->Position = Vector3(0,0,0);
    
    startGameButton->ToFront();
}

void SetupGame::Disabled() {
   pivot->GetComponent<Transform>()->Position = Vector3(-20,0,0);
}

GameObject* SetupGame::CreatePlayerFrame(GameObject** textBox) {

    Vector2 size(6.0f,0.5f);

    Gui* gui = Parent().gui;

    GameObject* player = gui->CreateControl(pivot, "Background", Vector3(0,-3,0), size);
    player->GetComponent<Transform>()->Anchor = size*0.5f;
    player->GetComponent<Transform>()->Position = Vector3(0,-10,0);
    
    GameObject* playerName = gui->CreateLabel(player, 0, size, Parent().font, "Jeppe", 0.5f);
    playerName->GetComponent<Label>()->HAlignment = Font::Center;
    playerName->GetComponent<Label>()->VAlignment = Font::Middle;
    
    player->AddComponent<TextBox>()->Text = "";
    playerName->AddComponent<TextBox>(player);
    
    *textBox = playerName;
    
    return player;
}

void SetupGame::StartButtonClicked(Nano::TouchData d) {

    SelectableCategorySystem::Categories categories = selectableCategories->GetActiveCategories();
    if (categories.empty()) return;
    
    
    for (auto it = categories.begin(); it!=categories.end(); ++it) {
        Parent().categoryFactory->CreateCategory(*(*it));
    }
    
    Parent().data.ResetPlayers();
    Parent().data.SelectRandomPlayer();
    Parent().data.IsGameFinished = false;
    
    for (int i=0; i<4; i++) {
        Parent().data.Players[i].Name = playerName[i]->GetComponent<Label>()->Text();
    }
    
    
    Parent().CurrentState = "SelectQuestion";
}


void SetupGame::CreateCategories() {
    if (categoriesCreated) return;
    
    CategoryDatabase& categoryDatabase = Parent().categoryDatabase;
    
    if (categoryDatabase.GetCategories().empty()) return;
    categoriesCreated = true;
    
    int amount = (int)categoryDatabase.GetCategories().size();
    
    Vector2 min(-6, 0);
    Vector2 max(1, 3);
    
    int across = 3;
    
    float dX = (max.x - min.x) / across;
    float dY = (max.y - min.y) / (floorf((float)amount / across)+1);
    
    Vector2 startPosition = min + Vector2(dX,dY) * 0.5f;
    
    int index = 0;
    for (auto it = categoryDatabase.GetCategories().begin(); it!=categoryDatabase.GetCategories().end(); ++it) {
        int x = index % across;
        int y = (int)floorf((float)index / across);
        
        Vector2 position = startPosition + Vector2(dX * x, dY * y);
        
        Parent().categoryFactory->CreateSelectableCategory((Category&)it->second, pivot, position);
        
        index++;
    }
}






